﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoragePeriod.IDAL.StorageForecast;
using System.Data.SqlClient;
using System.Data;
using StoragePeriod.DBUtility;
using StoragePeriod.Model.StorageForecast;

namespace StoragePeriod.SqlServerDAL.StorageForecast
{
    /// <summary>
    /// 数据访问类:QualityCalculateMove
    /// </summary>
    public partial class D_QualityCalculateMove : I_QualityCalculateMove
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from QualityCalculateMove");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_QualityCalculateMove model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into QualityCalculateMove(");
            strSql.Append("RawID,QuotaID,M,Connector,IsNegative,N,Z,A,B,CalculateTypeID)");
            strSql.Append(" values (");
            strSql.Append("@RawID,@QuotaID,@M,@Connector,@IsNegative,@N,@Z,@A,@B,@CalculateTypeID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@M", SqlDbType.Float,8),
					new SqlParameter("@Connector", SqlDbType.Int,4),
					new SqlParameter("@IsNegative", SqlDbType.Bit,1),
					new SqlParameter("@N", SqlDbType.Float,8),
					new SqlParameter("@Z", SqlDbType.Float,8),
					new SqlParameter("@A", SqlDbType.Float,8),
					new SqlParameter("@B", SqlDbType.Float,8),
					new SqlParameter("@CalculateTypeID", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.QuotaID;
            parameters[2].Value = model.M;
            parameters[3].Value = model.Connector;
            parameters[4].Value = model.IsNegative;
            parameters[5].Value = model.N;
            parameters[6].Value = model.Z;
            parameters[7].Value = model.A;
            parameters[8].Value = model.B;
            parameters[9].Value = model.CalculateTypeID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_QualityCalculateMove model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update QualityCalculateMove set ");
            strSql.Append("RawID=@RawID,");
            strSql.Append("QuotaID=@QuotaID,");
            strSql.Append("M=@M,");
            strSql.Append("Connector=@Connector,");
            strSql.Append("IsNegative=@IsNegative,");
            strSql.Append("N=@N,");
            strSql.Append("Z=@Z,");
            strSql.Append("A=@A,");
            strSql.Append("B=@B,");
            strSql.Append("CalculateTypeID=@CalculateTypeID");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@RawID", SqlDbType.Int,4),
					new SqlParameter("@QuotaID", SqlDbType.Int,4),
					new SqlParameter("@M", SqlDbType.Float,8),
					new SqlParameter("@Connector", SqlDbType.Int,4),
					new SqlParameter("@IsNegative", SqlDbType.Bit,1),
					new SqlParameter("@N", SqlDbType.Float,8),
					new SqlParameter("@Z", SqlDbType.Float,8),
					new SqlParameter("@A", SqlDbType.Float,8),
					new SqlParameter("@B", SqlDbType.Float,8),
					new SqlParameter("@CalculateTypeID", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.RawID;
            parameters[1].Value = model.QuotaID;
            parameters[2].Value = model.M;
            parameters[3].Value = model.Connector;
            parameters[4].Value = model.IsNegative;
            parameters[5].Value = model.N;
            parameters[6].Value = model.Z;
            parameters[7].Value = model.A;
            parameters[8].Value = model.B;
            parameters[9].Value = model.CalculateTypeID;
            parameters[10].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QualityCalculateMove ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from QualityCalculateMove ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_QualityCalculateMove GetModel(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,RawID,QuotaID,M,Connector,IsNegative,N,Z,A,B,CalculateTypeID from QualityCalculateMove ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_QualityCalculateMove model = new E_QualityCalculateMove();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RawID"].ToString() != "")
                {
                    model.RawID = int.Parse(ds.Tables[0].Rows[0]["RawID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["QuotaID"].ToString() != "")
                {
                    model.QuotaID = int.Parse(ds.Tables[0].Rows[0]["QuotaID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["M"].ToString() != "")
                {
                    model.M = decimal.Parse(ds.Tables[0].Rows[0]["M"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Connector"].ToString() != "")
                {
                    model.Connector = int.Parse(ds.Tables[0].Rows[0]["Connector"].ToString());
                }
                if (ds.Tables[0].Rows[0]["IsNegative"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsNegative"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsNegative"].ToString().ToLower() == "true"))
                    {
                        model.IsNegative = true;
                    }
                    else
                    {
                        model.IsNegative = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["N"].ToString() != "")
                {
                    model.N = decimal.Parse(ds.Tables[0].Rows[0]["N"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Z"].ToString() != "")
                {
                    model.Z = decimal.Parse(ds.Tables[0].Rows[0]["Z"].ToString());
                }
                if (ds.Tables[0].Rows[0]["A"].ToString() != "")
                {
                    model.A = decimal.Parse(ds.Tables[0].Rows[0]["A"].ToString());
                }
                if (ds.Tables[0].Rows[0]["B"].ToString() != "")
                {
                    model.B = decimal.Parse(ds.Tables[0].Rows[0]["B"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CalculateTypeID"].ToString() != "")
                {
                    model.CalculateTypeID = int.Parse(ds.Tables[0].Rows[0]["CalculateTypeID"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,RawID,QuotaID,M,Connector,IsNegative,N,Z,A,B,CalculateTypeID ");
            strSql.Append(" FROM QualityCalculateMove ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,RawID,QuotaID,M,Connector,IsNegative,N,Z,A,B,CalculateTypeID ");
            strSql.Append(" FROM QualityCalculateMove ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

    }
}
